package badugaclient;
/**
 * @author Wojtek N., Kamil M.
 * @version 0.7
 * Class which communicate between client and server.
 */
import java.io.*;
import java.net.*;
import javax.swing.SwingUtilities;

public class ClientServerCom 
{
	Socket server;
	private PrintWriter outToServer;
	private BufferedReader inFromServer;
	private String msgFromServer = "";
	private String nMsgFromServer = "";
	private Thread serverListener;
	
	String myID;
	String allPlayers;
	String startingTokens;
	String table;
	String smallBlind;
	String bigBlind;
	
	public void printClient(String s)
	{
		System.out.println(s);
	}
	/**
	 * Method which connects client with server (table).
	 * @param host name of host
	 * @param port number of the port
	 * @throws IOException
	 * @throws UnknownHostException
	 */
	public void connect(String host, int port) throws IOException, UnknownHostException
	{
		try
		{
			printClient("Trying to establish connection...");
			server = new Socket(host, port);
			printClient("Job's done!");
			inFromServer = new BufferedReader(new InputStreamReader (server.getInputStream()));
		}
		catch (UnknownHostException uhe)
		{
			System.err.println("Cannot recognize host.");
			uhe.printStackTrace();
		}
		catch (IOException ioe)
		{
			System.err.println("I/O failed. Printing where the problem is.");
			ioe.printStackTrace();
		}
	}
	
	public String getMsgFromServer()
	{
		return msgFromServer;
	}
	/**
	 * Method which depending on what action was taken, returns string to display on console.
	 * @return message depending on message from server
	 */
	public String display()
	{
		String action = msgFromServer.substring(1, 4);
		switch(action)
		{
			case "INI":
				msgFromServer = "Welcome! Your id is " + msgFromServer.substring(6,7)
								+ ".\nTable is preperaed for " + msgFromServer.substring(7, 8)
								+ " players.\nPlease wait for the rest of players.";
				break;
			case "W8T":
				msgFromServer = "Player " + msgFromServer.substring(6, 7)
								+ " joined our table.\nHurray! Only " + msgFromServer.substring(7, 8)
								+ " players left to start!";
				break;
			case "ERR":
				msgFromServer = msgFromServer.substring(6);
				break;
			default:
				printClient("Don't touch me! I'm working!");
				break;
		}
		return msgFromServer;
	}
	/**
	 * @return action sent from server
	 * @throws IOException
	 * @throws NullPointerException
	 */
	public String checkAction() throws IOException, NullPointerException
	{
		try
		{
			msgFromServer = inFromServer.readLine();
			return msgFromServer.substring(1, 4);
		}
		catch(IOException ioe)
		{
			System.err.println("I/O failed. Printing where the problem is.");
			ioe.printStackTrace();
		}
		catch(NullPointerException npe)
		{
			System.err.println("Don't try to read from something that, don't exists!");
			npe.printStackTrace();
		}
		return "Cause eclipse do not compile";
	}
	/**
	 * 
	 * @param inFromServer message sent from server from which action is extracted
	 * @return action sent from server
	 */
	public String checkAction(String inFromServer)
	{
		this.msgFromServer = inFromServer;
		return inFromServer.substring(1,4);
	}
	/**
	 * Method which disconnects client from server.
	 * @return message for console
	 * @throws IOException
	 */
	public String disconnect() throws IOException
	{
		try
		{
			server.close();
			return "Bye bye beutiful.";
		}
		catch(IOException ioe)
		{
			System.err.println("Whoops, someone was faster. Connection is already closed.");
			ioe.printStackTrace();
		}
		return "Cause eclipse do not compile";
	}
	/**
	 * Starts listener (thread) for a client, which receives messages from server.
	 * @param informator an observer
	 * @throws IOException
	 * @throws NullPointerException
	 */
	public void getWhatIsMine(final IInformation informator) throws IOException, NullPointerException
	{
		serverListener = new Thread(new Runnable()
						{
							@Override
							public void run()
							{
								try
								{
									nMsgFromServer = inFromServer.readLine();
									if(nMsgFromServer.equals("null"))
									{
										serverListener.interrupt();
									}
									SwingUtilities.invokeLater(new Runnable()
																{
																	@Override
																	public void run()
																	{
																		informator.messageReceived(nMsgFromServer);
																	}
																});
								}
								catch(IOException ioe)
								{
									System.err.println("I/O failed. Printing where the problem is.");
									SwingUtilities.invokeLater(new Runnable()
																{
																	@Override
																	public void run()
																	{
																		informator.connectionException();
																	}
																});
								}
								catch(NullPointerException npe)
								{
									System.err.println("Don't try to read from something that, don't exists!");
									SwingUtilities.invokeLater(new Runnable()
																{
																	@Override
																	public void run()
																	{
																		informator.nullpointerException();
																	}
																});
								}
							}
						});
		serverListener.start();
	}
	
	public void stopListen()
	{
		serverListener.interrupt();
	}
	
	public void sendMessage(String outToServer)// throws IOException
	{
		try
		{
			this.outToServer = new PrintWriter(server.getOutputStream(), true);
			this.outToServer.println(outToServer);
		}
		catch(IOException ioe)
		{
			System.err.println("I/O failed. Printing where the problem is.");
			ioe.printStackTrace();
		}
		
	}
	
	
}
